import 'dart:io';

import 'package:Sayagh/i18n/i18nLocale.dart';
import 'package:flutter/material.dart';
import 'package:progress_indicators/progress_indicators.dart';
import 'package:sqflite/sqflite.dart';
import 'package:url_launcher/url_launcher.dart';

String ourLocale = "fa";
final String website = "https://asnafhormozgan.ir/";
final String sayaghWebsite = "http://sayagh.asnafhormozgan.ir";
final String cities = "https://j.mp/asnaf30t1";
final String asnafStat = "asnafhormozgan.ir";
final String ourDatabase = "sayagh_db.db";
final String TablePressCsvPath = "wp-content/tables/";
final String drawerGoodUrl = "${sayaghWebsite}${TablePressCsvPath}drawer.csv";
/////Database
final String cityTableName = "TBL_CITIES";
final String kalaTable = "tbl_kala";
final String csvTableName = "tbl_csv";
final String drawerGoodTable = "DRAWER_GOOD";

final Widget asnafHeader = Image.asset(
  'assets/img/header.jpg',
  fit: BoxFit.fill,
  alignment: Alignment.centerRight,
);
launchURL(String url, {bool webView = false}) async {
  if (await canLaunch(url)) {
    await launch(url, forceWebView: webView);
  } else {
    throw 'Could not launch $url';
  }
}

final Widget loading = new Center(
  child: new Column(
    mainAxisAlignment: MainAxisAlignment.center,
    crossAxisAlignment: CrossAxisAlignment.center,
    children: <Widget>[
      Text(
        i18nLocale[ourLocale]['reciving'],
        style: TextStyle(fontFamily: "Sahel"),
      ),
      GlowingProgressIndicator(
        child: Icon(
          Icons.home,
          size: 100.0,
        ),
      ),
    ],
  ),
);

Future<bool> areWeConnectedToNet() async {
  try {
    final result = await InternetAddress.lookup(asnafStat);
    if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
      return true;
    }
  } on SocketException catch (_) {}
  return false;
}

Future openDb() async {
  await openDatabase(ourDatabase, version: 1,
      onCreate: (Database db, int version) async {
    String queryCity = '''
    CREATE TABLE IF NOT EXISTS "$cityTableName" (
      "id"	INTEGER PRIMARY KEY AUTOINCREMENT,
      "stateCode" Integer,
      "cityCode" INTEGER NOT NULL,
      "postCode"	INTEGER NOT NULL UNIQUE,
      "isActive"	INTEGER DEFAULT 1,
      "title"	TEXT NOT NULL UNIQUE,
      "fileName"	TEXT,
      "url" TEXT default 'http://sayagh.asnafhormozgan.ir',
      "hasSubTable"	INTEGER NOT NULL DEFAULT 0,
      "hasCategoryColumn"	INTEGER NOT NULL DEFAULT 0,
      "hasSubCategoryColumn"	INTEGER NOT NULL DEFAULT 0,
      "description"	TEXT
    );
    ''';
    await db.execute(queryCity);

    String queryTable = ''' CREATE TABLE IF NOT EXISTS  $kalaTable
(
	id integer
		constraint kala_pk
			primary key autoincrement,
	updateDate integer default current_timestamp,
	city text not null,
	tableName text not null,
	code text not null,
	name text not null,
	price int default 0 not null,
	describe text,
	unit text,
	category text,
	stateCode int,
	cityCode int,
	categoryCode int,
	kalaCode int,
	constraint kala_pk_2
		unique (id, updateDate)
);
''';
    await db.execute(queryTable);
    String queryCSV = '''
         create table IF NOT EXISTS $csvTableName
(
	id INTEGER
		constraint table_name_pk
			primary key autoincrement,
	isActive INTEGER default 1,
	title TEXT not null,
	fileName TEXT not null,
	url TEXT not null,
	description TEXT,
	hasSubTable INTEGER default 0,
	hasCategoryColumn INTEGER default 0,
	hasSubCategoryColumn INTEGER default 0,
	updateDate INTEGER default current_timestamp not null,
	signature TEXT not null,
	jsonData TEXT
);
create unique index table_name_signature_uindex
	on table_name (signature);
''';
    await db.execute(queryCSV);
    String queryDraweGood = '''
   CREATE TABLE IF NOT EXISTS "$drawerGoodTable" (
      "id"	INTEGER PRIMARY KEY AUTOINCREMENT,
      "isActive"	INTEGER NOT NULL DEFAULT 1,
      "title"	TEXT NOT NULL UNIQUE,
      "fileName"	TEXT,
      "hasSubTable"	INTEGER NOT NULL DEFAULT 0,
      "hasCategoryColumn"	INTEGER NOT NULL DEFAULT 0,
      "hasCategorySubColumn"	INTEGER NOT NULL DEFAULT 0,
      "url" TEXT default 'http://sayagh.asnafhormozgan.ir',
      "description"	TEXT
    );
    
    ''';

    await db.execute(queryDraweGood);
  });
}
